package com.hzz.openglstudy.render

import android.content.Context
import android.opengl.GLES20.*
import android.opengl.GLSurfaceView
import com.hzz.openglstudy.R
import com.hzz.openglstudy.util.compileFragmentShader
import com.hzz.openglstudy.util.compileVertexShader
import com.hzz.openglstudy.util.linkProgram
import com.hzz.openglstudy.util.readFileFromResource
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

/**
 * Created by hzz on 2018/6/13.
 */
class N2AirHockeyRender(private val context: Context) : GLSurfaceView.Renderer {
    companion object {
        const val POSITION_COMPONENT_COUNT = 2
        const val BYTES_PER_FLOAT = 4

        const val U_COLOR = "u_Color"
        const val A_POSITION = "a_Position"
    }

    private var vertexData: FloatBuffer
    private var uColorLocation = 0
    private var aPositionLocation = 0

    init {
        val tableVertices = floatArrayOf(
                //Triangle 1
                -0.5f, -0.5f,
                0.5f, 0.5f,
                -0.5f, 0.5f,
                //Triangle 2
                -0.5f, -0.5f,
                0.5f, -0.5f,
                0.5f, 0.5f,
                //line 1
                -0.5f, 0f,
                0.5f, 0f,
                //line 2
                0f, -0.25f,
                0f, 0.25f
        )

        vertexData = ByteBuffer.allocateDirect(tableVertices.size * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer()
        vertexData.put(tableVertices)
    }

    override fun onDrawFrame(gl: GL10?) {
        glClear(GL_COLOR_BUFFER_BIT)

        //画两个三角形
        glUniform4f(uColorLocation, 1.0f, 1.0f, 1.0f, 1.0f)
        glDrawArrays(GL_TRIANGLES,0,6)
        //画分割线
        glUniform4f(uColorLocation,1.0f,0.0f,0.0f,1.0f)
        glDrawArrays(GL_LINES,6,2)

        glUniform4f(uColorLocation, 0.0f, 0.0f, 1.0f, 1.0f)
        glDrawArrays(GL_POINTS,8,1)

        glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.1f)
        glDrawArrays(GL_POINTS,9,1)
    }


    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        glViewport(0, 0, width, height)
    }

    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        glClearColor(0.0f, 0.0f, 0.0f, 0f)

        val vertexShaderCode = context.readFileFromResource(R.raw.simple_vertex_shader)
        val fragmentShaderCode = context.readFileFromResource(R.raw.simple_fragment_shader)

        val vertexShader = compileVertexShader(vertexShaderCode)
        val fragmentShader = compileFragmentShader(fragmentShaderCode)

        val program = linkProgram(vertexShader, fragmentShader)
        glUseProgram(program)

        uColorLocation = glGetUniformLocation(program, U_COLOR)
        aPositionLocation= glGetAttribLocation(program, A_POSITION)

        vertexData.position(0)
        glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GL_FLOAT, false, 0, vertexData)

        glEnableVertexAttribArray(aPositionLocation)
    }
}